/**
 * 平台启动完毕后，需要初始化的内容(PC)
 */
(function() {
    // 取得LOGO、站点名称
    var logoF = function() {
        if (window.localStorage) {
            key = urlInfo.host + "/title";
            var v = localStorage.getItem(key);
            if (v) $("title").html(v);
            else {
                Classify.query("settings", "title", false, "", function(v) {
                    $("title").html(v);
                    localStorage.setItem(urlInfo.host + "/title", v);
                });
            }
        }
        else {
            Classify.query("settings", "title", false, "", function(v) {
                $("title").html(v);
            });
        }
    }

    // 公告
    var noticeF = function() {
    }

    // 缓存
    var cache = function() {
        var preLoadGame = function(g, i) {
            setTimeout(function() {
                preload_page(g);
            }, 200 * i);
        }
        for (var i = 0; i < 9; i++) {
            if (i >= GameList.menus.length) break;
            preLoadGame(GameList.menus[i].game, i);
        }
    };

    window.startup = function(cb) {
        // 账户信息改变的回调
        ME.cb = function() {
            set_account_info(ME.get_name(), ME.balance(), ME.pending());
        }
        ME.query();
        logoF();

        // 定期抓取赢的数据
        Util.registerTimer("bet_win_query", 5, function() {
            Result.bet_win_query();
        });
        Result.cb = function() {
            var c = Result.last();
            if (c)
                set_lastest_result(c.issue, c.red);
            else
                set_lastest_result();

            // 倒计时
            c = Result.curr();
            if (c) {
                var open = c.open / 1000,
                    end = c.end / 1000;
                context.curr[game] = c.issue;
                set_countdown(parseInt(open - get_server_time()), parseInt(end - get_server_time()), c.issue, c.betEnable);
            }
        };
                
        Lottery.cb = function() {
            var lottery = context.lottery[game];
            if (!isEmpty(lottery)) {
                // 填充进来
                var map = gameRule.map;
                for (var k in lottery) {
                    if (k == "targets") continue;
                    if (isEmpty(map[k])) {
                        console.log("不存在的类型", k);
                        continue;
                    }
                    map[k].rate = lottery[k].value;
                }
            }
            refresh_bet_items();
    
            // 填充下注列表
            refresh_curr_bet_list();
        };

        // 底部
        scan_counter_confirm();

        // 获取游戏列表
        GameList.init(function() {
            Result.pullLastOpen();
            Result.pullNext();
            
            cache();
            if (cb) cb();
        });

        // 掉线检测
        ME.assureConnect();
    }
})();
